package delegate;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;

import javax.naming.CommunicationException;
import javax.swing.JOptionPane;

import dto.*;
import interfaces.RemoteInterface;

public class BusinessDelegate {
	
	private static BusinessDelegate instance;
	private RemoteInterface remoteReference;
	
	private BusinessDelegate() throws CommunicationException
	{
		remoteConnection();
	}
	
	public static BusinessDelegate getInstance() throws CommunicationException
	{
		if(instance == null)
			instance = new BusinessDelegate();
		return instance;
	}

	private void remoteConnection() throws CommunicationException
	{
		try {
			remoteReference =  (RemoteInterface) Naming.lookup("//localhost/remoteBusiness");
		} catch (MalformedURLException | RemoteException | NotBoundException e) {
			throw new CommunicationException("Error establishing connection with Server.");
		}
	}
	
	// Asigno los metodos que se encuentran en la interfaz del common (remoteReference.metodo).
	
	public List<String> getMateriasPrimas() throws RemoteException{
		return remoteReference.getMateriasPrimas();
	}
	
	public List<String> getUnidades() throws RemoteException{
		return remoteReference.getUnidades();
	}
	
}